const {ipcMain} = require("electron");
const sshConn = require("./sshConn");
const init = require("./init");

const utf8 = require("utf8");
const SSHClient = require("ssh2").Client;

function handleIPC(win) {
  //Callback
  ipcMain.on("init", function (event) {
    console.log("handleIPC ipcMain.on init ");
    init(win);
  });
  ipcMain.on("terminal", function (event, data) {
    console.log("handleIPC ipcMain.on terminal main on: ", data);
    sshConn(win, data);
  });
  ipcMain.on("rendererToMainNoReturnValue1", function (event, arg1, arg2) {
    console.log("handleIPC ipcMain.on rendererToMainNoReturnValue1 main on: ", arg1, arg2);
  });
  ipcMain.on("rendererToMainWithReturnValue1", function (event, arg1, arg2) {
    console.log("ipcMain.on=====rendererToMainWithReturnValue1 main on: ", arg1, arg2);
    console.log("ipcMain.on-----rendererToMainWithReturnValue1 main reply: ", arg1, arg2);
    event.reply("onRendererToMainWithReturnValue1", arg1, arg2);
    event.reply("onRendererToRenderer", arg1, arg2);
  });
  ipcMain.on("rendererToMainThenMainToRenderer1", function (event, arg1, arg2) {
    console.log("rendererToMainThenMainToRenderer1 main on: ", arg1, arg2);
    console.log("rendererToMainThenMainToRenderer1 main send: ", arg1, arg2);
    win.webContents.send("onRendererToMainThenMainToRenderer1", arg1, arg2);
  });

  //Promise

  ipcMain.handle("rendererToMainNoReturnValue2", function (event, arg1, arg2) {
    console.log("rendererToMainNoReturnValue2 main handle: ", arg1, arg2);
  });
  ipcMain.handle(
    "rendererToMainWithReturnValue2",
    function (event, arg1, arg2) {
      console.log("rendererToMainWithReturnValue2 main handle: ", arg1, arg2);
      let args = { arg1, arg2 };
      console.log("rendererToMainWithReturnValue2 main return: ", args);
      return args;
    }
  );
  ipcMain.handle('getWebContentsId',function() {
    let winId =win.webContents.id
    console.log("getWebContentsId main handle ");
    console.log("getWebContentsId main return: ", winId);
    return winId
  })
}

module.exports = handleIPC;
